﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.InventoryMgtServiceRef;

    public class InvHold : BasePage
    {
        protected Button btnDelete;
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnSave;
        protected CheckBox chbIsHold;
        protected DropDownList ddl;
        protected DropDownList ddlHoldReasonCode;
        protected HtmlForm F5051;
        protected HtmlGenericControl fontLoc;
        protected HtmlGenericControl fontLot;
        protected HtmlGenericControl fontLpn;
        protected HtmlGenericControl fontOwner;
        protected HtmlGenericControl fontSkuID;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnHoldType;
        protected HtmlInputHidden hdnInvHoldID;
        protected HtmlInputHidden hdnIsHoldStatus;
        protected HtmlImage img1;
        protected HtmlImage imgLoc;
        protected HtmlImage imgLot;
        protected HtmlImage imgLpn;
        protected HtmlImage imgOwnerID;
        protected HtmlImage imgSkuID;
        protected HtmlImage imgTime;
        protected Label lbl;
        protected Label lblHoldBy;
        protected Label lblHoldDate;
        protected Label lblHoldReasonCode;
        protected Label lblHoldReasonDesrc;
        protected Label lblInvHoldID;
        protected Label lblLoc;
        protected Label lblLot;
        protected Label lblLpn;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblOwnerID;
        protected Label lblReleaseBy;
        protected Label lblReleaseDate;
        protected Label lblSkuDesrc;
        protected Label lblSkuID;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtHoldBy;
        protected TextBox txtHoldDate;
        protected TextBox txtHoldReasonDesrc;
        protected TextBox txtInvHoldID;
        protected TextBox txtLoc;
        protected TextBox txtLot;
        protected TextBox txtLpn;
        protected TextBox txtOwnerID;
        protected TextBox txtReleaseBy;
        protected TextBox txtReleaseDate;
        protected TextBox txtSkuDesrc;
        protected TextBox txtSkuID;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                InvHoldInfo invHoldInfo = base.DataEntityViewState as InvHoldInfo;
                List<string> invHoldIDs = new List<string> {
                    invHoldInfo.InvHoldID
                };
                if (invHoldInfo.IsHold == "Y")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5250_005");
                }
                else if (invHoldIDs.Count > 0)
                {
                    this.lblMsg.Text = InventoryMgtServiceClientProxy.GetInstance().DeleteInvHold(invHoldIDs) + base.GetGlobalMsgResource("C006");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("InvHold.aspx", false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string CS40000 = this.ddl.SelectedValue.ToUpper();
            if (CS40000 != null)
            {
                if (!(CS40000 == "LOTLOCLPN"))
                {
                    if (CS40000 == "OWNER")
                    {
                        if (string.IsNullOrEmpty(this.txtOwnerID.Text))
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F5250_002");
                            this.txtOwnerID.Focus();
                            return;
                        }
                    }
                    else if ((CS40000 == "SKU") && (string.IsNullOrEmpty(this.txtSkuID.Text) || string.IsNullOrEmpty(this.txtOwnerID.Text)))
                    {
                        if (string.IsNullOrEmpty(this.txtSkuID.Text))
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F5250_003");
                            this.txtSkuID.Focus();
                        }
                        else
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F5250_002");
                            this.txtOwnerID.Focus();
                        }
                        return;
                    }
                }
                else if ((string.IsNullOrEmpty(this.txtLoc.Text) && string.IsNullOrEmpty(this.txtLot.Text)) && string.IsNullOrEmpty(this.txtLpn.Text))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5250_001");
                    return;
                }
            }
            try
            {
                this.InsertInvHoldInfo();
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
            }
        }

        protected void chbIsHold_CheckedChanged(object sender, EventArgs e)
        {
            string temp = this.chbIsHold.Checked ? "Y" : "N";
            if (this.chbIsHold.Checked)
            {
                this.txtHoldDate.Text = base.GetPreferenceShortDate(DateTime.Now);
                this.txtHoldBy.Text = base.UserInfo.UserID;
            }
            else
            {
                this.txtReleaseBy.Text = base.UserInfo.UserID;
                this.txtReleaseDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            }
            if (this.hdnIsHoldStatus.Value == temp)
            {
                this.btnSave.Enabled = false;
            }
            else
            {
                this.btnSave.Enabled = true;
            }
        }

        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SetDDLSelectedValue(this.ddl.SelectedValue.ToUpper());
            this.hdnHoldType.Value = this.ddl.SelectedValue;
        }

        private InvHoldInfo GetInvHoldInfo()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as InvHoldInfo);
            }
            return (base.PageQueryEntitySession as InvHoldInfo);
        }

        private void GetSkuDescription()
        {
            SkuInfo skuInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.txtOwnerID.Text.Trim(), this.txtSkuID.Text.Trim(), "", "SKU") as SkuInfo;
            if (skuInfo != null)
            {
                this.txtSkuDesrc.Text = skuInfo.Descr;
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5250_006");
                this.txtSkuDesrc.Text = "";
            }
        }

        private void InsertInvHoldInfo()
        {
            try
            {
                InvHoldInfo invHolde = new InvHoldInfo();
                invHolde = this.SetInvHoldInfo(invHolde);
                invHolde.CreatedBy = base.UserInfo.UserID;
                string temp = InventoryMgtServiceClientProxy.GetInstance().ExecuteInvHold(invHolde);
                base.DataEntityViewState = invHolde;
                this.txtInvHoldID.Text = temp;
                this.hdnInvHoldID.Value = temp;
                this.lblMsg.Text = temp + base.GetGlobalMsgResource("C004");
                this.btnSave.Enabled = false;
                this.ddl.Enabled = false;
                this.hdnIsHoldStatus.Value = this.chbIsHold.Checked ? "Y" : "N";
                if (!this.chbIsHold.Enabled)
                {
                    this.chbIsHold.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("UNIQUE KEY"))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5250_004");
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
                }
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlHoldReasonCode, "HoldReason");
            base.BindData2DDLBySysCode(this.ddl, "HoldType");
            this.LotLocLpnSetTextBoxEnable();
            this.txtHoldDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            this.txtHoldBy.Text = base.UserInfo.UserID;
            this.txtReleaseDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnInvHoldID.Value = base.Request.QueryString["ID"].ToString();
                this.SetInvHoldInfo2Form();
                this.hdnFlag.Value = "1";
                this.hdnActionID.Value = this.hdnInvHoldID.Value;
                this.btnSave.Enabled = false;
            }
            else
            {
                this.SetDDLSelectedValue("BYLOTLOCLPN");
                this.chbIsHold.Enabled = false;
                this.fontLoc.Visible = true;
                this.fontLot.Visible = true;
                this.fontLpn.Visible = true;
                this.fontOwner.Visible = false;
                this.fontSkuID.Visible = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        private void LotLocLpnSetTextBoxEnable()
        {
            this.txtSkuID.Enabled = false;
            this.txtSkuID.Text = "";
            this.imgSkuID.Disabled = true;
            this.txtOwnerID.Enabled = false;
            this.txtOwnerID.Text = "";
            this.imgOwnerID.Disabled = true;
            this.txtLot.Enabled = true;
            this.imgLot.Disabled = false;
            this.txtLoc.Enabled = true;
            this.imgLoc.Disabled = false;
            this.txtLpn.Text = "";
            this.txtLpn.Enabled = true;
            this.imgLpn.Disabled = false;
            this.txtSkuID.CssClass = "rdtextbox";
            this.txtOwnerID.CssClass = "rdtextbox";
            this.txtLot.CssClass = "textbox";
            this.txtLoc.CssClass = "textbox";
            this.txtLpn.CssClass = "textbox";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetDDLSelectedValue(string type)
        {
            string CS40000 = type;
            if (CS40000 != null)
            {
                if (!(CS40000 == "LOTLOCLPN"))
                {
                    if (CS40000 == "OWNER")
                    {
                        this.txtSkuID.Enabled = false;
                        this.txtSkuID.Text = "";
                        this.txtSkuDesrc.Text = "";
                        this.imgSkuID.Disabled = true;
                        this.txtOwnerID.Enabled = true;
                        this.imgOwnerID.Disabled = false;
                        this.txtLot.Enabled = false;
                        this.txtLot.Text = "";
                        this.imgLot.Disabled = true;
                        this.txtLoc.Enabled = false;
                        this.txtLoc.Text = "";
                        this.imgLoc.Disabled = true;
                        this.txtLpn.Text = "";
                        this.txtLpn.Enabled = false;
                        this.imgLpn.Disabled = true;
                        this.txtSkuID.CssClass = "rdtextbox";
                        this.txtOwnerID.CssClass = "textbox";
                        this.txtLot.CssClass = "rdtextbox";
                        this.txtLoc.CssClass = "rdtextbox";
                        this.txtLpn.CssClass = "rdtextbox";
                        this.fontLoc.Visible = false;
                        this.fontLot.Visible = false;
                        this.fontLpn.Visible = false;
                        this.fontOwner.Visible = true;
                        this.fontSkuID.Visible = false;
                    }
                    else if (CS40000 == "SKU")
                    {
                        this.txtSkuID.Enabled = true;
                        this.imgSkuID.Disabled = false;
                        this.txtOwnerID.Enabled = true;
                        this.imgOwnerID.Disabled = false;
                        this.txtLot.Enabled = false;
                        this.txtLot.Text = "";
                        this.imgLot.Disabled = true;
                        this.txtLoc.Enabled = false;
                        this.txtLoc.Text = "";
                        this.imgLoc.Disabled = true;
                        this.txtLpn.Enabled = false;
                        this.txtLpn.Text = "";
                        this.imgLpn.Disabled = true;
                        this.txtSkuID.CssClass = "textbox";
                        this.txtOwnerID.CssClass = "textbox";
                        this.txtLot.CssClass = "rdtextbox";
                        this.txtLoc.CssClass = "rdtextbox";
                        this.txtLpn.CssClass = "rdtextbox";
                        this.fontLoc.Visible = false;
                        this.fontLot.Visible = false;
                        this.fontLpn.Visible = false;
                        this.fontOwner.Visible = true;
                        this.fontSkuID.Visible = true;
                    }
                }
                else
                {
                    this.LotLocLpnSetTextBoxEnable();
                    this.txtOwnerID.Text = "";
                    this.txtSkuID.Text = "";
                    this.txtSkuDesrc.Text = "";
                    this.fontLoc.Visible = true;
                    this.fontLot.Visible = true;
                    this.fontLpn.Visible = true;
                    this.fontOwner.Visible = false;
                    this.fontSkuID.Visible = false;
                }
            }
        }

        private InvHoldInfo SetInvHoldInfo(InvHoldInfo invHoldInfoEnitity)
        {
            InvHoldInfo invHoldInfo = invHoldInfoEnitity;
            DateTime dt = DateTime.Now;
            if (DateTime.TryParse(this.txtHoldDate.Text.Trim(), out dt))
            {
                invHoldInfo.HoldDate = DateTime.Parse(this.txtHoldDate.Text.Trim());
            }
            else
            {
                invHoldInfo.HoldDate = DateTime.Now;
            }
            invHoldInfo.InvHoldID = this.txtInvHoldID.Text.Trim();
            invHoldInfo.Lot = this.txtLot.Text.Trim();
            invHoldInfo.OwnerID = this.txtOwnerID.Text.Trim();
            invHoldInfo.HoldBy = this.txtHoldBy.Text.Trim();
            invHoldInfo.Loc = this.txtLoc.Text.Trim();
            invHoldInfo.SkuID = this.txtSkuID.Text.Trim();
            if (DateTime.TryParse(this.txtReleaseDate.Text.Trim(), out dt))
            {
                invHoldInfo.ReleasedDate = DateTime.Parse(this.txtReleaseDate.Text.Trim());
            }
            else
            {
                invHoldInfo.ReleasedDate = DateTime.Now;
            }
            invHoldInfo.LpnID = this.txtLpn.Text.Trim();
            invHoldInfo.ReleasedBy = this.txtReleaseBy.Text.Trim();
            invHoldInfo.HoldReasonCode = this.ddlHoldReasonCode.SelectedValue;
            invHoldInfo.HoldReasonDesc = this.txtHoldReasonDesrc.Text.Trim();
            invHoldInfo.HoldType = this.ddl.SelectedValue;
            invHoldInfo.IsHold = this.chbIsHold.Checked ? "Y" : "N";
            invHoldInfo.UpdatedBy = base.UserInfo.UserID;
            return invHoldInfo;
        }

        private void SetInvHoldInfo2Form()
        {
            try
            {
                string holdType = "BYLOTLOCLPN";
                string InvHoldID = this.hdnInvHoldID.Value.ToString();
                InvHoldInfo invHoldInfo = InventoryMgtServiceClientProxy.GetInstance().GetInvHoldInfoByID(InvHoldID);
                base.DataEntityViewState = invHoldInfo;
                if (invHoldInfo != null)
                {
                    this.ddl.SelectedValue = invHoldInfo.HoldType;
                    this.ddl_SelectedIndexChanged(null, null);
                    this.txtInvHoldID.Text = invHoldInfo.InvHoldID;
                    this.txtHoldDate.Text = base.GetPreferenceShortDate(invHoldInfo.HoldDate);
                    this.txtLot.Text = invHoldInfo.Lot;
                    this.txtOwnerID.Text = invHoldInfo.OwnerID;
                    this.txtHoldBy.Text = invHoldInfo.HoldBy;
                    this.txtLoc.Text = invHoldInfo.Loc;
                    this.txtSkuID.Text = invHoldInfo.SkuID;
                    this.txtReleaseDate.Text = base.GetPreferenceShortDate(invHoldInfo.ReleasedDate);
                    this.txtLpn.Text = invHoldInfo.LpnID;
                    this.txtReleaseBy.Text = invHoldInfo.ReleasedBy;
                    this.ddlHoldReasonCode.SelectedValue = invHoldInfo.HoldReasonCode;
                    this.txtHoldReasonDesrc.Text = invHoldInfo.HoldReasonDesc;
                    this.ddl.Enabled = false;
                    this.chbIsHold.Checked = invHoldInfo.IsHold == "Y";
                    this.hdnIsHoldStatus.Value = invHoldInfo.IsHold;
                    this.hdnHoldType.Value = this.ddl.SelectedValue;
                }
                this.SetDDLSelectedValue(holdType);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void txtOwnerID_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()) && (this.ddl.SelectedValue.ToUpper() == "SKU"))
            {
                if (!string.IsNullOrEmpty(this.txtSkuID.Text.Trim()))
                {
                    this.GetSkuDescription();
                }
                else
                {
                    this.txtSkuID.Focus();
                }
            }
        }

        protected void txtSkuID_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5250_002");
                this.txtOwnerID.Focus();
            }
            else if (string.IsNullOrEmpty(this.txtSkuID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5250_003");
                this.txtSkuID.Focus();
            }
            else
            {
                this.GetSkuDescription();
            }
        }
    }
}

